﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LengthUnit
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        double sb = 0;
        public double[] temp = new double[10];
        public string[] temp1 = new string[10];
        public string[] temp2 = new string[10];
        public double[] temp3 = new double[10];
        int i = 0;
        int flag = 0;
        bool Decimal_flag = true;
        bool Delete_flag = true;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        { 
            temp[0] = 1;
            temp[1] = 0.1;
            temp[2] = 0.001;
            temp[3] = 39.3701;
            temp[4] = 0.0393701;
            temp[5] = 0.00328084;

            temp[6] = 1000.0F; //um
        }
        int t;
        //按键为: 非 ( 大键盘0~9 || 小键盘 0~9 || 小数点 || 退格键  ) 时不可输入
        private void mm_PreviewKeyDown(object sender, KeyEventArgs e)
        {   
             if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.Tab || e.Key == Key.Enter))
            { 
                e.Handled = true;//不可输入 
            }
            else
            {  
                try
                { 
                    if (e.Key == Key.Decimal)
                    {
                        Decimal_flag = true;
                    }
                    else
                    {
                        Decimal_flag = false;
                        t = ((int)e.Key - 4) % 10;
                    }

                }
                catch
                {

                }

            }
        }

        private void cm_PreviewKeyDown(object sender, KeyEventArgs e)
        {

            if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.Tab || e.Key == Key.Enter))
            {
                e.Handled = true;//不可输入 
            }
            else
            {
                try
                {
                    if (e.Key == Key.Decimal)
                    {
                        Decimal_flag = true;
                    }
                    else
                    {
                        Decimal_flag = false;
                        t = ((int)e.Key - 4) % 10;
                    }

                }
                catch
                {

                }

            }
        } 

        private void m_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.Tab || e.Key == Key.Enter))
            {
                e.Handled = true;//不可输入 
            }
            else
            {
                try
                {
                    if (e.Key == Key.Decimal)
                    {
                        Decimal_flag = true;
                    }
                    else
                    {
                        Decimal_flag = false;
                        t = ((int)e.Key - 4) % 10;
                    }

                }
                catch
                {

                }

            }
        }

        private void mil_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.Tab || e.Key == Key.Enter))
            {
                e.Handled = true;//不可输入 
            }
            else
            {
                try
                {
                    if (e.Key == Key.Decimal)
                    {
                        Decimal_flag = true;
                    }
                    else
                    {
                        Decimal_flag = false;
                        t = ((int)e.Key - 4) % 10;
                    }

                }
                catch
                {

                }

            }
        }

        private void inch_PreviewKeyDown(object sender, KeyEventArgs e)
        { 
            if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.Tab || e.Key == Key.Enter))
            {
                e.Handled = true;//不可输入 
            }
            else
            {
                try
                {
                    if (e.Key == Key.Decimal)
                    {
                        Decimal_flag = true;
                    }
                    else
                    {
                        Decimal_flag = false;
                        t = ((int)e.Key - 4) % 10;
                    }

                }
                catch
                {

                }

            }
        }

        private void ft_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.Tab || e.Key == Key.Enter))
            {
                e.Handled = true;//不可输入 
            }
            else
            {
                try
                {
                    if (e.Key == Key.Decimal)
                    {
                        Decimal_flag = true;
                    }
                    else
                    {
                        Decimal_flag = false;
                        t = ((int)e.Key - 4) % 10;
                    }

                }
                catch
                {

                }

            }
        }

        private void um_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.Tab || e.Key == Key.Enter))
            {
                e.Handled = true;//不可输入 
            }
            else
            {
                try
                {
                    if (e.Key == Key.Decimal)
                    {
                        Decimal_flag = true;
                    }
                    else
                    {
                        Decimal_flag = false;
                        t = ((int)e.Key - 4) % 10;
                    }

                }
                catch
                {

                }

            }
        }


        private void mm_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                temp3[0] = double.Parse(mm.Text) / temp[0];
                for (i = 1; i < 7; i++)
                    temp3[i] = temp3[0] * temp[i];
                if (Decimal_flag == true)
                    mm.Text = temp3[0].ToString() + ".".ToString();
                else
                    mm.Text = temp3[0].ToString();
                cm.Text = temp3[1].ToString("F3");
                m.Text = temp3[2].ToString("F3");
                mil.Text = temp3[3].ToString("F3");
                inch.Text = temp3[4].ToString("F3");
                ft.Text = temp3[5].ToString("F3");
                um.Text = temp3[6].ToString("F3");

            }
            catch
            {

            }
        }

        private void cm_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                temp3[0] = double.Parse(cm.Text) / temp[1];
                for (i = 1; i < 7; i++)
                    temp3[i] = temp3[0] * temp[i];

                mm.Text = temp3[1].ToString("F3");
                if (Decimal_flag == true)
                    cm.Text = temp3[0].ToString() + ".".ToString();
                else
                    cm.Text = temp3[0].ToString();
                m.Text = temp3[2].ToString("F3");
                mil.Text = temp3[3].ToString("F3");
                inch.Text = temp3[4].ToString("F3");
                ft.Text = temp3[5].ToString("F3");
                um.Text = temp3[6].ToString("F3");

            }
            catch
            {

            }
        }
        private void m_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                temp3[0] = double.Parse(m.Text) / temp[2];
                for (i = 1; i < 7; i++)
                    temp3[i] = temp3[0] * temp[i];

                mm.Text = temp3[0].ToString("F3");
                cm.Text = temp3[1].ToString("F3");
                if (Decimal_flag == true)
                    m.Text = temp3[2].ToString() + ".".ToString();
                else
                    m.Text = temp3[2].ToString();
                mil.Text = temp3[3].ToString("F3");
                inch.Text = temp3[4].ToString("F3");
                ft.Text = temp3[5].ToString("F3");
                um.Text = temp3[6].ToString("F3");

            }
            catch
            {

            }
        }

        private void mil_KeyUp(object sender, KeyEventArgs e)
        { 
            try
            {
                temp3[0] = double.Parse(mil.Text) / temp[3];
                for (i = 1; i < 7; i++)
                    temp3[i] = temp3[0] * temp[i];

                mm.Text = temp3[0].ToString("F3");
                cm.Text = temp3[1].ToString("F3");
                m.Text = temp3[2].ToString("F3");
                if (Decimal_flag == true)
                    mil.Text = temp3[3].ToString() + ".".ToString();
                else
                    mil.Text = temp3[3].ToString();
                inch.Text = temp3[4].ToString("F3");
                ft.Text = temp3[5].ToString("F3");
                um.Text = temp3[6].ToString("F3");

            }
            catch
            {

            }
        }

        private void inch_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                temp3[0] = double.Parse(inch.Text) / temp[4];
                for (i = 1; i < 7; i++)
                    temp3[i] = temp3[0] * temp[i];

                mm.Text = temp3[0].ToString("F3");
                cm.Text = temp3[1].ToString("F3");
                m.Text = temp3[2].ToString("F3");
                mil.Text = temp3[3].ToString("F3");
                if (Decimal_flag == true)
                    inch.Text = temp3[4].ToString() + ".".ToString();
                else
                    inch.Text = temp3[4].ToString();
                ft.Text = temp3[5].ToString("F3");
                um.Text = temp3[6].ToString("F3");

            }
            catch
            {

            }
        }

        private void ft_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                temp3[0] = double.Parse(ft.Text) / temp[5];
                for (i = 1; i < 7; i++)
                    temp3[i] = temp3[0] * temp[i];

                mm.Text = temp3[0].ToString("F3");
                cm.Text = temp3[1].ToString("F3");
                m.Text = temp3[2].ToString("F3");
                mil.Text = temp3[3].ToString("F3");
                inch.Text = temp3[4].ToString("F3");
                if (Decimal_flag == true)
                    ft.Text = temp3[5].ToString() + ".".ToString();
                else
                    ft.Text = temp3[5].ToString();
                um.Text = temp3[6].ToString("F3");

            }
            catch
            {

            }
        }

        private void um_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                temp3[0] = double.Parse(um.Text) / (double)temp[6];
                for (i = 1; i < 7; i++)
                    temp3[i] = temp3[0] * temp[i];

                mm.Text = temp3[0].ToString("F3");
                cm.Text = temp3[1].ToString("F3");
                m.Text = temp3[2].ToString("F3");
                mil.Text = temp3[3].ToString("F3");
                inch.Text = temp3[4].ToString("F3");
                ft.Text = temp3[5].ToString("F3");
                if (Decimal_flag == true)
                    um.Text = temp3[6].ToString() + ".".ToString();
                else
                    um.Text = temp3[6].ToString();

            }
            catch
            {

            }
        }

    }
}
